/**
 * @ author: 汪孟苹
 */
define(['jquery',"bbcare","comm/form/form","bch/app/patientManager/lookInfo.servers"],function($,$bb,$form,$servers){
	var lookInfo = {};

	/**
	 * 建档成功后的回调函数
	 */
	var ajaxSubmitTaskCallBack = function(data){
		$bb.toastShow("修改成功！");
	}

	/**
	 * 建档保存
	 */
	var onArchiveSave = function(){
		if( $form.validate("archiveBox") ){
			var datas = $(this).data("params");
			var doctorName = JSON.parse($bb.getSessionStorage("account")).alias;
			var channelId = $bb.getChannelId();
			var contList = $form.getSaveData("archiveBox");
			var userName = $bb.code.getValueByName("archiveBox","userName");
			var params = {
				"oprInfo":{
					"opCode":"1001",
					"opType":"2",
					"opNote":"【"+doctorName+"】医生修改【"+userName+"】患者建档信息",
					"channelType":"2", // web端门诊渠道:2
					"channelId":channelId,
				},
				"taskInfo":{
					"ptTaskId":datas.ptTaskId,
					"contList":contList,
					"userId":datas.userId
				},
				"saveType":"1"
			};
			$servers.ajaxSubmitTask(params,ajaxSubmitTaskCallBack);
		}
	}

	/**
	 * 患者临床路径任务内容查询
	 */
	var ajaxContCallBack = function(data,datas){
		var questions = data.contList ? data.contList : data.taskList;
		if( questions && questions.length > 0 ){
			$form.init({
				"id":"archiveBox",
				"questions":questions,
				"setStatus":$servers.getSetStatus(),
				"values":data.valueList,
				"event":{}
			});
			// 保存
			var topbars = [
				{
					"icon":"fa fa-floppy-o",
					"name":"保存",
					"params":datas,
					"callback":onArchiveSave
				}
			];
	        $bb.toolbar(topbars);
		}
	}

	/**
	 * 患者临床路径节点下任务查询成功后的回调函数
	 */
	var ajaxTasksCallBack = function(data,datas){
		if( data.rows.length > 0 ){
			datas.ptTaskId = data.rows[0].id;
			$servers.ajaxCont(data.rows[0],ajaxContCallBack,datas);
		}
	}

	/**
	 * 患者临床路径（有效）节点查询成功后的回调函数
	 */
	var ajaxUserCpNodesCallBack = function(data,params){
		if( data.rows.length > 0 ){
			var datas = {};
			datas.userId = params.userId;
			$servers.ajaxTasks(data.rows[0],ajaxTasksCallBack,datas);
		}
	}

	/**
	 * 建档页面初始化
	 */
	lookInfo.init = function(){
		var params = $bb.getPageParams();
		$servers.ajaxUserCpNodes(params,ajaxUserCpNodesCallBack);
	};

	return lookInfo;
});